public class LinkedList<T> {
    private Node<T> head;
    private int length;
    public LinkedList(){
        this.length = 0;
        head = new Node<T>(null);
    }
    public Node<T> getHead(){
        return this.head;
    }
    public boolean add(T obj,int pos){
        if(pos < 1 || pos >length+1){
            System.out.println("pos 值不合法");
            return false;
        }
        int num = 1;
        Node<T> p = head;
        Node<T> q = head.next;
        while (num < pos){
            p = q;
            q = q.next;
            num++;
        }
        Node<T> s = new Node<T>(obj,null);
        s.next = q;
        p.next = s;
        length++;
        return true;
    }
    public T remove(int pos){
        if(isEmpty()){
            System.out.println("链表为空表");
        }
        if(pos < 1 || pos >length+1){
            System.out.println("pos 值不合法");
            return null;
        }
        int num = 1;
        Node<T> p = head;
        Node<T> q=head.next;
        while (num < pos){
            p =  q;
            q = q.next;
            num++;
        }
        p.next = q.next;
        length --;
        return q.data;
    }
    public T value(int pos){
        if(isEmpty()){
            System.out.println("链表为空表");
            return null;
        }
        if(pos < 1 || pos >length+1){
            System.out.println("pos 值不合法");
            return null;
        }
        int num = 1;
        Node<T> q = head.next;
        while (num<pos){
            q = q.next;
            num++;
        }
        return q.data;
    }
    public  int find(T obj){
        if(isEmpty()){
            System.out.println("链表为空表");
            return -1;
        }
        int num = 1;
        Node<T> p = head.next;
        while (p != null){
            if(!p.data.equals(obj)){
                p = p.next;
                num++;
            }else {
                break;
            }
        }
        if(p == null){
            return  -1;
        }
        return num;
    }
    public boolean modify(T obj,int pos){
        if(isEmpty()){
            System.out.println("链表为空表");
            return false;
        }
        if(pos < 1 || pos >length+1){
            System.out.println("pos 值不合法");
            return false;
        }
        int num = 1;
        Node<T> q = head.next;
        while (num<pos){
            q = q.next;
            num++;
        }
        q.data = obj;
        return true;
    }
    public  boolean isEmpty(){
        return this.length == 0;
    }
    public  int size(){
        return this.length;
    }
    public  void nextOrder(){
        int num = 1;
        Node<T> q = head.next;
        while (q != null){
            System.out.print(q.data + "\t");
            q = q.next;
        }
        System.out.println();
    }
    public  void  clear(){
        length = 0;
        head.next = null;
    }

}
